/*
 * @Author: ShiJunJie
 * @Date: 2021-02-01 16:26:05
 * @LastEditors  : ShiJunJie
 * @LastEditTime : 2022-04-20 11:15:49
 * @Descripttion: 用户相关 APi 接口
 */

import https from '@/utils/https';
import storage from 'good-storage';

import { message } from 'ant-design-vue';

const api = {
  user: '/user/v1/'
};

export type USERType = any;

export const User = {
  /** 用户登录 */
  login: async (data: any, type: any) => {
    // const res = await https.post(`/login/${type}`, { data })
    const res = { code: 0, data: { userId: 1 } };
    if (res.code === 0) {
      console.log('登录返回体', res.data);
      if (res.data.userId) {
        // message.info(`${JSON.stringify(res.data)}`)
        storage.set('USER_TOKEN', { ...res.data, type });
        const routerData = await User.getRouter();
        // const routerData = await User.getRouterByUserId(res.data.userId)
        if (!routerData.code) {
          storage.set('USER_ROUTERS', routerData.data);
        }
      }
    }
    return res;
  },
  refresh: async () => {
    // const res = await https.put(`/manage/v1/access`, { data })
    //todo fakeData
    const res: any = {
      code: 0,
      data: {
        account: '11',
        name: '某用户',
        token: (Math.random() * 100).toString(),
        exp: new Date().getTime() + 600000
      }
    }
    if (res.code === 0) {
      console.log('刷新刷新', res.data)
      if (res.data.account) {
        // message.info(`${JSON.stringify(res.data)}`)
        storage.set('USER_TOKEN', {
          ...res.data,
        })
      }
    }
    return res
  },
  getRouterByUserId: (id: any) => https.get(`account/${id}/menu`),
  getRouter: () => {
    const res = [
      {
        id: 3,
        name: '演示demo',
        close: false,
        // path: "/admin/home",
        icon: 'icon-layers-o',
        meta: { menu: true },
        children: [
          {
            meta: { menu: true },
            id: '30',
            name: '测试界面',
            path: '/demo/test/page'
          },
          {
            id: '31',
            name: 'demo1',
            path: '/demo/test/demo1'
          },
          {
            id: '32',
            name: 'demo2',
            path: '/demo/test/demo2'
          }
        ]
      }
    ];
    storage.set('USER_ROUTERS', res);
    return { code: 0, data: res };
  },
  loginOut: (data: any) => https.get('/account/login/out', { data })
};
export const verify = {
  get: (data: any) => https.get(`/image/verify/${data}`),
  post: (data: any) => https.post(`/image/verify`, { data })
};

export const ACCOUNT = {
  /** 修改密码 */
  setPassword: (data: any) => https.patch('/account/password', { data }),
  /** 找回密码 */
  forgetPassword: (data: any) => https.post('/login/forget/change/password', { data })
};
